﻿namespace HutongGames.PlayMakerEditor
{
    using HutongGames.PlayMaker;
    using System;
    using UnityEditor;
    using UnityEngine;

    public class StateSelector : EditorWindow
    {
        private static bool autoScroll;
        private FsmState lastSelectedState;
        private static Vector2 scrollPosition;
        private static float scrollViewHeight;
        private static Rect selectedRect;

        private void DoAutoScroll()
        {
            if ((FsmEditor.SelectedState != null) && ((Event.current.type == EventType.Repaint) && autoScroll))
            {
                scrollViewHeight = GUILayoutUtility.GetLastRect().height;
                if (selectedRect.y < 0f)
                {
                    scrollPosition.y += selectedRect.y;
                    base.Repaint();
                }
                else if ((selectedRect.y + selectedRect.height) > scrollViewHeight)
                {
                    scrollPosition.y += (selectedRect.y + selectedRect.height) - scrollViewHeight;
                    base.Repaint();
                }
                autoScroll = false;
            }
        }

        private static void DoStateRow(FsmState state)
        {
            GUIContent content = new GUIContent(state.Name, state.Name);
            if (FsmEditorGUILayout.TableRow(new GUIContent[] { content }, new float[] { 1f }, FsmEditor.SelectedState == state, FsmErrorChecker.StateHasErrors(state), new GUILayoutOption[0]) >= 0)
            {
                SelectState(state);
                GUIUtility.ExitGUI();
            }
            if ((state == FsmEditor.SelectedState) && (Event.current.type == EventType.Repaint))
            {
                selectedRect = GUILayoutUtility.GetLastRect();
                selectedRect.y -= scrollPosition.y;
            }
        }

        private static void DoToolbar()
        {
            GUILayout.BeginHorizontal(EditorStyles.toolbar, new GUILayoutOption[0]);
            if (GUILayout.Button(FsmEditorUtility.GetFullFsmLabel(FsmEditor.SelectedFsm), EditorStyles.toolbarDropDown, new GUILayoutOption[0]))
            {
                FsmEditorGUILayout.GenerateFsmSelectionMenu(false).ShowAsContext();
            }
            GUILayout.EndHorizontal();
        }

        private static void HandleKeyboardInput()
        {
            if (GUIUtility.keyboardControl == 0)
            {
                int controlID = GUIUtility.GetControlID(FocusType.Keyboard);
                if (Event.current.GetTypeForControl(controlID) == EventType.KeyDown)
                {
                    switch (Event.current.keyCode)
                    {
                        case KeyCode.UpArrow:
                            Event.current.Use();
                            SelectPrevious();
                            GUIUtility.ExitGUI();
                            return;

                        case KeyCode.DownArrow:
                            Event.current.Use();
                            SelectNext();
                            GUIUtility.ExitGUI();
                            return;
                    }
                }
            }
        }

        private void OnEnable()
        {
            base.title = "State Browser";
            base.minSize = new Vector2(200f, 100f);
            base.autoRepaintOnSceneChange = true;
        }

        private void OnGUI()
        {
            if (FsmEditorGUILayout.ToolWindowsCommonGUI(this))
            {
                if (((Event.current.type == EventType.MouseDown) && (Event.current.button == 0)) && (GUIUtility.hotControl == 0))
                {
                    GUIUtility.keyboardControl = 0;
                }
                HandleKeyboardInput();
                DoToolbar();
                scrollPosition = GUILayout.BeginScrollView(scrollPosition, new GUILayoutOption[0]);
                if (FsmEditor.SelectedFsm != null)
                {
                    foreach (FsmState state in FsmEditor.SelectedFsm.States)
                    {
                        DoStateRow(state);
                    }
                }
                GUILayout.EndScrollView();
                this.DoAutoScroll();
                FsmEditorGUILayout.Divider(new GUILayoutOption[0]);
                GUILayout.BeginHorizontal(new GUILayoutOption[0]);
                GUILayout.FlexibleSpace();
                GUILayout.EndHorizontal();
                EditorGUILayout.Space();
            }
        }

        private static void SelectNext()
        {
            if (FsmEditor.SelectedState != null)
            {
                int index = Array.IndexOf<FsmState>(FsmEditor.SelectedFsm.States, FsmEditor.SelectedState);
                if (index < (FsmEditor.SelectedFsm.States.Length - 1))
                {
                    SelectState(FsmEditor.SelectedFsm.States[index + 1]);
                }
                autoScroll = true;
            }
        }

        private static void SelectPrevious()
        {
            if (FsmEditor.SelectedState != null)
            {
                int index = Array.IndexOf<FsmState>(FsmEditor.SelectedFsm.States, FsmEditor.SelectedState);
                if (index > 0)
                {
                    SelectState(FsmEditor.SelectedFsm.States[index - 1]);
                }
                autoScroll = true;
            }
        }

        private static void SelectState(FsmState state)
        {
            FsmEditor.SelectState(state, true);
            FsmEditor.RepaintAll();
        }

        private void Update()
        {
            if (!FsmEditorSettings.DisableEditorWhenPlaying && (this.lastSelectedState != FsmEditor.SelectedState))
            {
                this.lastSelectedState = FsmEditor.SelectedState;
                autoScroll = true;
                base.Repaint();
            }
        }
    }
}

